package com.ruoyi.livedemo.controller;

import com.ruoyi.livedemo.common.ApiResponse;
import com.ruoyi.livedemo.context.CurrentUserHolder;
import com.ruoyi.livedemo.dto.ProfileUpdateRequest;
import com.ruoyi.livedemo.dto.UserProfileView;
import com.ruoyi.livedemo.service.AuthService;
import com.ruoyi.livedemo.service.ProfileService;
import javax.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/profile")
public class ProfileController {

    private final ProfileService profileService;
    private final AuthService authService;

    @GetMapping
    public ApiResponse<UserProfileView> getProfile() {
        Long userId = CurrentUserHolder.getUserId();
        return ApiResponse.ok(profileService.loadProfile(userId));
    }

    @PutMapping
    public ApiResponse<UserProfileView> updateProfile(@Valid @RequestBody ProfileUpdateRequest request) {
        Long userId = CurrentUserHolder.getUserId();
        UserProfileView view = profileService.upsertProfile(userId, request);
        return ApiResponse.ok("个人信息已更新", view);
    }

    @PatchMapping("/theme")
    public ApiResponse<Void> updateTheme(@RequestParam String theme) {
        Long userId = CurrentUserHolder.getUserId();
        profileService.updateTheme(userId, theme);
        return ApiResponse.ok("主题偏好已更新", null);
    }

    @PatchMapping("/display-name")
    public ApiResponse<Void> updateDisplayName(@RequestParam String displayName) {
        Long userId = CurrentUserHolder.getUserId();
        authService.updateDisplayName(userId, displayName);
        return ApiResponse.ok("昵称已更新", null);
    }
}
